﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OreSystem.Entities;
using OreSystem.BLL;
using OreSystem.Common;
namespace MainFrom
{
    public partial class ItemEdit : Form
    {
        public int Type { get; set; }
        public Item NewItem { get; set; }
        private CarBLL carBll = new CarBLL();
        private ItemBLL itemBll = new ItemBLL();
        private bool newItemFlag = true;
        public ItemEdit()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            dgv_CarNo.DataSource = null;
            if (carBll.GetCarByType(this.Type , txt_CarNo.Text).Count > 0) {
                dgv_CarNo.DataSource = carBll.GetCarByType(this.Type, txt_CarNo.Text);
                dgv_CarNo.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
                 dgv_CarNo.Show();
            }
            else
            {
                dgv_CarNo.Hide();
            }
           
                       
 
        }

        private void ItemEdit_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            lbl_msg.ForeColor = Color.Red;        
            Dtp_Item.Format = DateTimePickerFormat.Custom;
            Dtp_Item.CustomFormat = "yyyy-MM-dd";
            cmb_CalType.Items.Add(new { Text = "拉土", Value = "1" });
            cmb_CalType.Items.Add(new {Text="拉渣",Value="0"});
            //白班夜班
            cmb_WorkType.Items.Add(new { Text = "白班", Value = "0" });
            cmb_WorkType.Items.Add(new { Text = "夜班", Value = "1" });
            cmb_CalType.ValueMember = "Value";
            cmb_CalType.DisplayMember = "Text";
            cmb_WorkType.ValueMember = "Value";
            cmb_WorkType.DisplayMember = "Text";
            cmb_CalType.SelectedIndex = 0;
            cmb_WorkType.SelectedIndex = 0;
            
            if (this.Type == 1)
            {
                dgv_CarNo.Columns.Add("ID", "序号");
                dgv_CarNo.Columns.Add("Type","类型");
                dgv_CarNo.Columns.Add("CarNO", "车编号");
                dgv_CarNo.Columns.Add("LNO", "车牌号");
                dgv_CarNo.Columns[0].DataPropertyName = "ID";
                dgv_CarNo.Columns[1].DataPropertyName = "Type";
                dgv_CarNo.Columns[2].DataPropertyName = "CarNO";
                dgv_CarNo.Columns[3].DataPropertyName = "LicensePlate";
                label5.Hide();
                cmb_CalType.Hide();
            }
            else if (this.Type == 2) 
            {
                dgv_CarNo.Columns.Add("ID", "序号");
                dgv_CarNo.Columns.Add("Type", "类型");
                dgv_CarNo.Columns.Add("CarNO", "车编号");
                dgv_CarNo.Columns.Add("LNO", "车牌号");
                dgv_CarNo.Columns[0].DataPropertyName = "ID";
                dgv_CarNo.Columns[1].DataPropertyName = "Type";
                dgv_CarNo.Columns[2].DataPropertyName = "CarNO";
                dgv_CarNo.Columns[3].DataPropertyName = "LicensePlate";
                label4.Hide();
                txtLenght.Hide();
                txtLenght.Text = "0";
            }            
            dgv_CarNo.AllowUserToAddRows = false;
            dgv_CarNo.AllowUserToDeleteRows = false;
            dgv_CarNo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgv_CarNo.EditMode = DataGridViewEditMode.EditProgrammatically;
            
            dgv_CarNo.AutoGenerateColumns = false;
            dgv_CarNo.MultiSelect = false;
            if (NewItem.ID != 0) {
                newItemFlag = false;
                txt_CarNo.Text = NewItem.CarNumber.ToString();
                txt_CNo.Text = NewItem.CarID.ToString();
                txt_Times.Text = NewItem.times.ToString();
                Dtp_Item.Value = NewItem.CreateDate;
                txtLenght.Text = NewItem.Lenght.ToString();
                cmb_CalType.SelectedIndex = (int)NewItem.CalType;
            }
            txt_CNo.Visible = false;
            dgv_CarNo.Hide();  
        }

        private void dgv_CarNo_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            string carNo = dgv_CarNo.CurrentRow.Cells[0].Value.ToString();
            txt_CarNo.Text = carNo;
            dgv_CarNo.DataSource = null;
            dgv_CarNo.Hide();
        }      

        private void ItemEdit_Click(object sender, EventArgs e)
        {
            dgv_CarNo.DataSource = null;
            dgv_CarNo.Hide();
        }

        private void txt_CarNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            //MessageBox.Show(e.KeyChar.ToString());
            if (e.KeyChar == (char)13)
            {
                dgv_CarNo.Focus();
            }
        }

        private void dgv_CarNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            //if (e.KeyChar == (char)13)
            //{
            //    string carId = dgv_CarNo.CurrentRow.Cells[0].Value.ToString();
            //    txt_CarNo.Text = carId;
            //    dgv_CarNo.DataSource = null;
            //    dgv_CarNo.Hide();
            //    Dtp_Item.Focus();
            //}            
            //return ;
        }

        private void button1_Click(object sender, EventArgs e) 
        {
            if (string.IsNullOrEmpty(txt_CarNo.Text)){
                lbl_msg.Text = "没有选择车辆";
                txt_CarNo.Focus();
                return;
            }
            if (!ValidationRegex.VadidateData(txt_CNo.Text))
            {
                lbl_msg.Text = "汽车信息错误。请在列表中选择！";
                txt_Times.Focus();
                return;
            }
            if (carBll.GetCarById(Convert.ToInt32(txt_CNo.Text)) == null)
            {
                lbl_msg.Text = "汽车信息不存在,请选择正确的";
                txt_CarNo.Focus();
                return;
            }
            if (newItemFlag && itemBll.GetItemByCarId(Convert.ToInt32(txt_CNo.Text), Dtp_Item.Value, this.Type) != null)
            {
                lbl_msg.Text = "当天车辆信息已经添加";
                txt_CarNo.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txt_Times.Text)) {
                //MessageBox.Show("车次为零");
                lbl_msg.Text = "车次不能为空";
                txt_Times.Focus();
                return;
            }
            if (!ValidationRegex.VadidateData(txt_Times.Text))
            {
                lbl_msg.Text = "车次输入的不是数字";
                txt_Times.Focus();
                return;
            }
            if (string.IsNullOrEmpty(txtLenght.Text))
            {
                //MessageBox.Show("车次为零");
                lbl_msg.Text = "距离不能为空";
                txt_Times.Focus();
                return;
            }
            if (this.Type == 1 && !ValidationRegex.VadidateData(txtLenght.Text))
            {
                lbl_msg.Text = "距离输入的不是数字";
                txt_Times.Focus();
                return;
            }
            NewItem.CarID = Convert.ToInt32(txt_CNo.Text);
            NewItem.CarNumber = txt_CarNo.Text;
            NewItem.times = Convert.ToInt32(txt_Times.Text);
            NewItem.CreateDate = Dtp_Item.Value;
            NewItem.Type = this.Type;
            NewItem.Lenght = Convert.ToDecimal(txtLenght.Text);
            NewItem.CalType = (short)cmb_CalType.SelectedIndex;
            NewItem.WorkType = (short)cmb_WorkType.SelectedIndex;
            try {
                itemBll.Save(NewItem);
                lbl_msg.Text = "保存成功！";
               
            }
            catch (Exception ex) {
                MessageBox.Show("出现错误请联系管理员。错误信息" + ex.Message);
                return;
            }
            if(newItemFlag)
            {
                this.NewItem = new Item();
                lbl_msg.Text = "新数据添加成功！请继续添加";
                txt_CarNo.Text = "";
                txt_Times.Text = "";
                txt_CarNo.Focus();
            }
            else {
                MessageBox.Show("保存成功！");
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e) {
            txt_CarNo.Text = "";
            txt_Times.Text = "";
            
        }

        private void txt_Times_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                if (this.Type == 1)
                {
                    txtLenght.Focus();
                }
                else
                {
                    cmb_CalType.Focus();
                }
            }
        }

        private void Dtp_Item_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == (char)13) {
                txt_Times.Focus();                
            }
        }
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
            if (keyData == Keys.Enter) {
                if (dgv_CarNo.Focused) {
                    string carId = dgv_CarNo.CurrentRow.Cells[0].Value.ToString();
                    txt_CarNo.Text = dgv_CarNo.CurrentRow.Cells[2].Value.ToString();
                    txt_CNo.Text = carId ;
                    dgv_CarNo.DataSource = null;
                    dgv_CarNo.Hide();
                    Dtp_Item.Focus();
                }
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void txt_CarNo_Enter(object sender, EventArgs e) {
            dgv_CarNo.DataSource = null;
            dgv_CarNo.Hide();
        }

        private void txtLenght_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1.Focus();
            }
        }

        private void cmb_CalType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                button1.Focus();
            }
        }
    }
}
